const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
  const AdView = sequelize.define('AdView', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    user_id: {
      type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false,
      references: {
        model: 'users',
        key: 'id'
      }
    },
    ad_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '广告ID'
    },
    ad_type: {
      type: DataTypes.STRING(30),
      allowNull: false,
      comment: '广告类型：video, banner, popup'
    },
    duration: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '观看时长（秒）'
    },
    points_earned: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '获得的积分'
    },
    device_id: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '设备ID'
    },
    ip_address: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'IP地址'
    },
    status: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 1,
      comment: '状态：1-有效，0-无效（作弊）'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  }, {
    tableName: 'ad_views',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  });

  return AdView;
};